$(document).ready(function(){	
	$("#form-member").validate(
		{
			errorElement: 'span', //default input error message container
			errorClass: 'help-inline', // default input error message class
			focusInvalid: false, // do not focus the last invalid input
			ignore: "",
			highlight: function (element) { // hightlight error inputs
				$(element)
					.closest('.help-inline').removeClass('ok'); // display OK icon
				$(element)
					.closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
			},

			unhighlight: function (element) { // revert the change dony by hightlight
				$(element)
					.closest('.control-group').removeClass('error'); // set error class to the control group
			},

			success: function (label) {
				label
					.addClass('valid').addClass('help-inline ok') // mark the current input as valid and display OK icon
				.closest('.control-group').removeClass('error').addClass('success'); // set success class to the control group
			}
		}
	);
		
	$(".select2").select2();
	$('#btn-date').datepicker()
		.on('show', function(ev){
			var date = $("#year").val()+"-"+$("#month").val()+"-"+$("#day").val();
			$('#btn-date').data({date: date});
			$('#btn-date').datepicker('update');
		})
		.on('changeDate', function(ev){
			$('#btn-date').datepicker('hide');
			
			var date = new Date(ev.date);
			$("#year").select2("val", date.getFullYear());
			$("#month").select2("val", date.getMonth()+1);
			$("#day").select2("val", date.getDate());
		});
})

var FormWizard = function () {


    return {
        //main function to initiate the module
        init: function () {
            if (!jQuery().bootstrapWizard) {
                return;
            }

            // default form wizard
            $('#form_wizard_1').bootstrapWizard({
                'nextSelector': '.button-next',
                'previousSelector': '.button-previous',
                onTabClick: function (tab, navigation, index) {
                    alert('on tab click disabled');
                    return false;
                },
                onNext: function (tab, navigation, index) {
					if(index > 1){
						var $valid = $("div#tab"+index+" :input").valid();
						if(!$valid) {
							$("div#tab"+index+" :input").focusInvalid();
							return false;
						}
					}
                    var total = navigation.find('li').length;
                    var current = index + 1;
                    // set wizard title
                    $('.step-title', $('#form_wizard_1')).text('Step ' + (index + 1) + ' of ' + total);
                    // set done steps
                    jQuery('li', $('#form_wizard_1')).removeClass("done");
                    var li_list = navigation.find('li');
                    for (var i = 0; i < index; i++) {
                        jQuery(li_list[i]).addClass("done");
                    }

                    if (current == 1) {
                        $('#form_wizard_1').find('.button-previous').hide();
                    } else {
                        $('#form_wizard_1').find('.button-previous').show();
                    }

                    if (current >= total) {
                        $('#form_wizard_1').find('.button-next').hide();
                        $('#form_wizard_1').find('.button-submit').show();
                    } else {
                        $('#form_wizard_1').find('.button-next').show();
                        $('#form_wizard_1').find('.button-submit').hide();
                    }
                    App.scrollTo($('.page-title'));
                },
                onPrevious: function (tab, navigation, index) {
                    var total = navigation.find('li').length;
                    var current = index + 1;
                    // set wizard title
                    $('.step-title', $('#form_wizard_1')).text('Step ' + (index + 1) + ' of ' + total);
                    // set done steps
                    jQuery('li', $('#form_wizard_1')).removeClass("done");
                    var li_list = navigation.find('li');
                    for (var i = 0; i < index; i++) {
                        jQuery(li_list[i]).addClass("done");
                    }

                    if (current == 1) {
                        $('#form_wizard_1').find('.button-previous').hide();
                    } else {
                        $('#form_wizard_1').find('.button-previous').show();
                    }

                    if (current >= total) {
                        $('#form_wizard_1').find('.button-next').hide();
                        $('#form_wizard_1').find('.button-submit').show();
                    } else {
                        $('#form_wizard_1').find('.button-next').show();
                        $('#form_wizard_1').find('.button-submit').hide();
                    }

                    App.scrollTo($('.page-title'));
                },
                onTabShow: function (tab, navigation, index) {
                    var total = navigation.find('li').length;
                    var current = index + 1;
                    var $percent = (current / total) * 100;
                    $('#form_wizard_1').find('.bar').css({
                        width: $percent + '%'
                    });
                }
            });

            $('#form_wizard_1').find('.button-previous').hide();
            $('#form_wizard_1 .button-submit').click(function () {
                var $valid = $("#form-member").valid();
				if(!$valid) {
					$("#form-member").focusInvalid();
					return false;
				}
				
				FormWizard.sendData();
            }).hide();
        },
		//send data
		sendData: function(){
			$.ajax({
				url: base_url+"registration",
				data : $("#form-member").serialize(),
				beforeSend: function(){
					var el = jQuery("body");
					App.blockUI(el);
				},
				success: function(respon) {
					var el = jQuery("body");
					App.unblockUI(el);
					
					try {
						var obj = jQuery.parseJSON(respon);
						
						if(obj.status == "success"){
							$("#form-member").hide();
							$(".alert-success").show();
							
							App.scrollTo($('.page-title'));
						}else if(obj.status == "error"){
							$('#form_wizard_1').bootstrapWizard("show",(obj.step-1));
							var navigationLength = $('#form_wizard_1').bootstrapWizard("navigationLength");
							if((obj.step-1) < navigationLength){
								$('#form_wizard_1').find('.button-next').show();
								$('#form_wizard_1').find('.button-submit').hide();
							}
							
							jQuery('li', $('#form_wizard_1')).removeClass("done");
							var li_list = jQuery('li', $('#form_wizard_1')).removeClass("done");
							for (var i = 0; i < obj.step-1; i++) {
								jQuery(li_list[i]).addClass("done");
							}
					
							$.each(obj.msg, function(i, dt) {
								$("#"+dt.id)
									.closest('.help-inline').removeClass('ok');
								$("#"+dt.id)
									.closest('.control-group').removeClass('success').addClass('error'); // set error class to the control group
								$("#"+dt.id).parent().children(".help-inline").text(dt.text);
							})
						}
					} catch (e) {
						// error
						return false;
					}
				},
				type: "post",
				dataType: "html"
			});
		}

    };

}();